/**
 * 公司客服情况
 * - 使用自定义封装组件，简化代码，统一管理。
 * - 2025-10-13 16:47:02 tzj
 * */

import BaseComponent from "../../components/BaseComponent";
import {mrhzApi} from "../../services/basic";
import {addPrecise} from "../../utils/math";

// 要显示的form表单
const formItemList = [
    "companies",
    "personZw",
    "personZw",
    "check",
    {
        name: "dateRange",
        label: "留资日期",
        start: "qsrq",
        end: "jsrq"
    },
];
// 显示的按钮
const buttons = ["query", "export",];
// 表格列信息
const columns = [
    {
        title: '公司/人员',//xm
        tip: '公司名、销售/客服/负责人',
        dataIndex: 'gsmc',
        // width: "20%",
        sumName: "合计"
    },
    {
        title: '总到店',
        tip: "到店已成交+到店未成交+异公司到店已成交+异公司到店未成交",
        dataIndex: 'zdd',
        render: (_, record) => Number(record.ddwcj) + Number(record.ddycj) + Number(record.ygscj) + Number(record.ygsdd),
        formula: "=SUM({{ddwcj}},{{ddycj}},{{ygscj}},{{ygsdd}})",
        sorter: true
    },
    {
        title: '到店未成交',
        dataIndex: 'ddwcj',
        sorter: true
    },
    {
        title: '到店已成交',
        dataIndex: 'ddycj',
        sorter: true
    },
    {
        title: '到店已成交总金额',
        tip: '包括本店+异店成交',
        dataIndex: 'cjje',
        sorter: true
    },
    {
        title: '异公司已成交',
        dataIndex: 'ygscj',
        sorter: true
    },
    {
        title: '异公司到店未成交',
        dataIndex: 'ygsdd',
        sorter: true
    },
    {
        title: '成交率（%）',
        dataIndex: 'wxcjl',
        tip: '成交率 = （本店+异店）成交 ÷ 总到店 × 100%',
        render: (_, record) => {
            const zcj = Number(record.ddycj) + Number(record.ygscj)
            const zdd = Number(record.ddwcj) + Number(record.ygsdd) + zcj
            return (zcj * 100 / zdd).toFixed(2)
        },
        numFmt: "0.00%",
        formula: "=({{ddycj}}+{{ygscj}})/{{zdd}}",
        sorter: true,
    },
    {
        title: '客单价',
        tip: '客单价 = 到店已成交总金额/（异公司成交数+本店到店成交）',
        render: (_, record) => {
            const denominator = (Number(record.ddycj) + Number(record.ygscj))
            return denominator ? (Number(record.cjje) / denominator).toFixed(2) : 0
        },
        numFmt: "0.00",
        formula: "=({{cjje}}/({{ddycj}}+{{ygscj}})",
        dataIndex: 'wxkdj',
        sorter: true
    },
    {
        title: '有效留资',
        dataIndex: 'yxlz',
        sorter: true
    },
    {
        title: '到店比',
        tip: '到店比=有效留资数/总到店数',
        dataIndex: 'ddb',
        render: (_, record) => {
            const zdd = Number(record.ddwcj) + Number(record.ddycj) + Number(record.ygscj) + Number(record.ygsdd)
            if (!zdd) return 0
            return (Number(record.yxlz) / Number(zdd)).toFixed(2)
        },
        numFmt: "0.00",
        formula: "=IFERROR({{yxlz}}/{{zdd}}, 0)",
        sorter: true
    },
]

class pageGscjtj extends BaseComponent {
    constructor(props) {
        super(props);
        this.state = {
            ...this.state,
            formItemList,
            buttons,
            // columns,
            filename: "鹏成车邦分公司客服留资情况报表",
            tableTitle: true,
            queryAll: true,
            sumRow: true,
            expandable: {
                expandRowByClick: true,
                rowExpandable: (record) => !!record.children, // 可选：控制哪些行可以展开
            }
        }
        this.columns = columns;
        this.queryApi = mrhzApi.selectPagegsry;
        this.hasLzModal = true;
    }

    dataProcess(arr) {
        console.log(arr)
        const map = new Map();

        arr.forEach(item => {
            const obj = {
                ...item,
                gsmc: item.xm,
                jgmc: item.jgmc,
            }
            if (map.has(item.gsmc)) {
                const existing = map.get(item.gsmc);
                existing.ddwcj = addPrecise((existing.ddwcj || 0) , (obj.ddwcj || 0))
                existing.ddycj = addPrecise((existing.ddycj || 0) , (obj.ddycj || 0))
                existing.ygscj = addPrecise((existing.ygscj || 0) , (obj.ygscj || 0))
                existing.ygsdd = addPrecise((existing.ygsdd || 0) , (obj.ygsdd || 0))
                existing.cjje = addPrecise((existing.cjje || 0) , (obj.cjje || 0))
                existing.yxlz = addPrecise((existing.yxlz || 0) , (obj.yxlz || 0))
                existing.children.push(obj);
            } else {
                map.set(item.gsmc, {
                    ...item,
                    gsmc: item.gsmc,
                    children: [obj],
                });
            }
        });

        const fn = (group, pk) => {
            const arr = [...group.children]
            arr.forEach((item, index) => {
                item.key = pk + index
                group.ddwcj = addPrecise((group.ddwcj || 0) , (item.ddwcj || 0))
                group.ddycj = addPrecise((group.ddycj || 0) , (item.ddycj || 0))
                group.ygscj = addPrecise((group.ygscj || 0) , (item.ygscj || 0))
                group.ygsdd = addPrecise((group.ygsdd || 0) , (item.ygsdd || 0))
                group.cjje = addPrecise((group.cjje || 0) , (item.cjje || 0))
                group.yxlz = addPrecise((group.yxlz || 0) , (item.yxlz || 0))
            })
        }

        // 转换Map为数组并添加层级key
        const list = Array.from(map.values()).map((parent, parentIndex) => {
            const parentKey = (parentIndex + 1).toString();

            const wxGroup = {
                key: parentKey + '-wx',
                gsmc: '微信客服',
                children: parent.children.filter(item => item.zw === '6')
            }
            console.log(wxGroup)

            fn(wxGroup, parentKey + '-wx');

            const xsGroup = {
                key: parentKey + '-xs',
                gsmc: '销售导购',
                children: parent.children.filter(item => item.zw === '2')
            }
            fn(xsGroup, parentKey + '-xs')

            const children = []
            if (wxGroup.children.length > 0) children.push(wxGroup)
            if (xsGroup.children.length > 0) children.push(xsGroup)

            return {
                ...parent,
                key: parentKey,
                children
            };
        });
        console.log(list)
        return list
    }

    onRowDoubleClick = (record) => {
        if (record.children) return;
        const params = {
            ...this.form.dataForm,
            yybm: record.yybm,
            sfzh: record.zhbh,
            lxr: record.gh,
            // lxr: 'KF013'
        }
        params.zzrq = params.jsrq;
        delete params.jsrq;
        this.lzModalRef.open(params, {});
    }
}

export default pageGscjtj;
